package com.bweab.controll.userstbcontrol;

import com.bweab.pojo.UsersTb;
import com.bweab.service.api.UsersTbServiceInterface;
import com.bweab.service.impl.UsersTbServiceInterfaceImpl;
import com.bweab.util.JSONResult;
import com.bweab.util.JSONStats;
import com.bweab.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/checkOldPwd")
public class CheckUserOldPwd extends HttpServlet {
    //创建服务对象
    UsersTbServiceInterface usersService=new UsersTbServiceInterfaceImpl();
    //创建返回对象
    JSONResult jsonResult = new JSONResult();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //相当于没有获取到数据
        //获取用户账号和密码
        String username=req.getParameter("username");
        String userpassword=req.getParameter("userpassword");
        System.out.println(username+"\n"+userpassword);
        if(username!=null || username !=""){
            //账号和密码不为空才去调用service
            UsersTb userInfo = usersService.getUserInfo(username);
            //判断密码是否相等
            if(userInfo.getUserPassword().equals(userpassword)){
                //相等的情况下
                jsonResult.setMessage(JSONStats.USER_PASSWORD_OK.getMessage());
                jsonResult.setStatusCode(JSONStats.USER_PASSWORD_OK.getStatusCode());
            }else {
                //不相等的情况下
                jsonResult.setStatusCode(JSONStats.USER_PASSWORD_ERROR.getStatusCode());
                jsonResult.setMessage(JSONStats.LOGIN_PASSWORD_ERROR.getMessage());
            }
        }else {
                jsonResult.setData(JSONStats.USER_ERROR.getMessage());
                jsonResult.setStatusCode(JSONStats.USER_ERROR.getStatusCode());
        }

        WebUtil.writeJson(resp,jsonResult);



    }
}
